{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cb1f9b3ecf45e0c5",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# Pandas初体验"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9265b7f0f0a556a",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 1. 准备动作, 即: 加载文件\n",
    "pandas解释: 它是Python的1个第三方模块, 可以实现: 数据的采集, 存储, 预处理, 分析, 可视化等操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89093bb9ff8dfdd1",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import os\n",
    "print(os.getcwd())  # current work directory: 当前的工作目录, 改为: 项目的目录即可.\n",
    "\n",
    "# 解决中文显示问题，下面的代码只需运行一次即可\n",
    "import matplotlib as plt\n",
    "plt.rcParams['font.sans-serif'] = ['sans-serif']\n",
    "plt.rcParams['axes.unicode_minus'] = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78a24a0e367c3fe5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-20T09:26:44.176393300Z",
     "start_time": "2024-12-20T09:26:44.159835Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 1. Pandas读取csv文件, 获取到 DataFrame对象.\n",
    "df = pd.read_csv('./data/1960-2019全球GDP数据.csv', encoding='gbk')\n",
    "df.head(10) # 查看前10行数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e70b15a38c0bbf4",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 2. 绘制中国的近年的GDP变化曲线."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ffe84264424da07",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 1.从上述的 df对象中, 获取到 中国的所有的数据. \n",
    "china_df = df[df.country == '中国']\n",
    "# 2. 打印df对象\n",
    "china_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d17ef72891bcb13a",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 3. 设置年份(year)列为 -> 索引列. \n",
    "# 参1: 索引列名.  参2: 是否在原数据上修改, 默认是False(不修改), 改为True: 直接修改源数据.\n",
    "china_df.set_index('year', inplace=True)\n",
    "# 4. 绘制中国GDP的变化曲线.\n",
    "china_df.GDP.plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1bdb68de91cb41c",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 3.分别查询中国、美国、日本三国的GDP数据，并绘制GDP变化曲线、进行对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9c374ec897f53d8b",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 1. 链式编程, 获取三个国家的GDP数据.\n",
    "china_df = df[df.country=='中国'].set_index('year')\n",
    "usa_df = df[df.country=='美国'].set_index('year')\n",
    "jp_df = df[df.country=='日本'].set_index('year')\n",
    "# 2. 打印三个国家的数据.\n",
    "print(china_df.head())\n",
    "print(usa_df.head())\n",
    "print(jp_df.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21c4c616ad92a7f3",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 3. 绘制中美日三国的GDP曲线, 如下的方式, 会绘制三张图.\n",
    "china_df.plot()\n",
    "usa_df.plot()\n",
    "jp_df.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e1ab250898f0693f",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 4. 绘制中美日三国的GDP曲线, 如下的方式, 绘制到1张图中.\n",
    "china_df.GDP.plot()\n",
    "usa_df.GDP.plot()\n",
    "jp_df.GDP.plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6122e38e8efc022",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 4. 绘制中美日三国GDP数据, 加入 图例(英文)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89dd4fbe9cb61f9",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 1. 链式编程, 获取三个国家的GDP数据.\n",
    "china_df = df[df.country=='中国'].set_index('year')\n",
    "usa_df = df[df.country=='美国'].set_index('year')\n",
    "jp_df = df[df.country=='日本'].set_index('year')\n",
    "\n",
    "# 核心细节: 修改列名. rename()函数即可.\n",
    "china_df.rename(columns={'GDP':'China'}, inplace=True)  # 修改源数据\n",
    "usa_df.rename(columns={'GDP':'USA'}, inplace=True) \n",
    "jp_df.rename(columns={'GDP':'Jp'}, inplace=True) \n",
    "\n",
    "# 2. 打印三个国家的数据.\n",
    "print(china_df.head())\n",
    "print(usa_df.head())\n",
    "print(jp_df.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "857a2ec45c87b69b",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 3. 绘制中美日三国的GDP曲线, 增加颜色 和 图例.\n",
    "china_df.China.plot(color='red', legend=True)\n",
    "usa_df.USA.plot(color='blue', legend=True)\n",
    "jp_df.Jp.plot(color='gray', legend=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8361b83a5672efc2",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 5. 绘制中美日三国GDP数据, 加入 图例(中文)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e93d2e55ea63560",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 1. 链式编程, 获取三个国家的GDP数据.\n",
    "china_df = df[df.country=='中国'].set_index('year')\n",
    "usa_df = df[df.country=='美国'].set_index('year')\n",
    "jp_df = df[df.country=='日本'].set_index('year')\n",
    "\n",
    "# 核心细节: 修改列名. rename()函数即可.\n",
    "china_df.rename(columns={'GDP':'中国'}, inplace=True)  # 修改源数据\n",
    "usa_df.rename(columns={'GDP':'美国'}, inplace=True) \n",
    "jp_df.rename(columns={'GDP':'日本'}, inplace=True) \n",
    "# 绘制中美日三国的GDP曲线, 增加颜色 和 图例.\n",
    "china_df.中国.plot(color='red', legend=True)\n",
    "usa_df.美国.plot(color='blue', legend=True)\n",
    "jp_df.日本.plot(color='gray', legend=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ai",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
